<?

class id_fetch extends fetch {
	public $data;
	public $count;
	
	// Houses Remaining
	public $remaining;
	private $house_id;
	private $broker_id;
	
	// Current Page number
	private $page;
	
	// For single run function only (boolan)
	private $startup;
	
	// Timers
	private $time;
	private $time1;
	
	// Size of Data
	public $data_size;
	
	function __construct() {
		// Includes
		require_once("classes/time.class.php");
		require_once("classes/curl_class.php");
		require_once("mysql.php");

		// Start Timer
		$this->time1 = new timer;
		
		// Define different varibles
		$this->page = 1;
		$this->startup = true;
	}
	
	function execute ($url) 
	{
		$this->time = new timer;
		
		// initialize Curl
		$curl = new curl();

			// Setup Url
			$curl->setup($url."&page=".$this->page);

			// Getting Respons
			// Import Data
			$this->data = $curl->execute();
			$this->data_size += mb_strlen($this->data);

			// Close Connection
			$curl->close();

		// Print Console Messages
		if($this->startup == true) {
			$this->console();
			$this->startup = false;
		}

		// Fetch IDS from HTML page
		$this->get_ids();

		// Insert them into a database
		$this->insert_ids();

		// Test then to stop script
		if($this->remaining > 0) {
			$this->page++;

			// Function 
			$this->execute($url);
		}
		else 
		{
			// Write to console
			$this->console_add("------------------------------------------------------------------------\n|   All IDS Processed");
		}
	}
	
	function console () {
		$this->get_total();
		$this->console_print();
	}
	
	function get_total () {
		$pattern = "/p;([0-9]+)/";
		preg_match($pattern, $this->data, $matches);
		
		if(is_numeric($matches[1])) {
			$this->remaining = $matches[1];
		} else {
			exit("No total found");
		}
	}
	
	function get_ids () {
		//$pattern = "/propertyid=([0-9A-Za-z]+[^&])&brokerid=([0-9A-Za-z]+[^&])/";
		$pattern = "/propertyid=([^&amp;]+)&amp;brokerid=([0-9A-Za-z][^&']+)/";
		preg_match_all($pattern, $this->data, $matches);

		// Remove unuseable data
		unset($matches[0]);
		
		// Remove dublicates
		$matches[1] = array_unique($matches[1]);
		
		// Insert to global array
		$this->house_id = $matches[1];
		$this->broker_id = $matches[2];
	}
	
	function insert_ids () {
		// Count entries
		$count = count($this->house_id);
		
		// Get key entries in array for match with broker_ids
		$key = array_keys($this->house_id);
		
		// Insert Count
		$found = 0;
		$processed = 0;
		
		// Insert into database
		for($i=0;$i<$count;$i++)
		{
			// Select Current Array entry
			$current_house_id = current($this->house_id);
			
			// Current array ID
			$current_broker_id = $this->broker_id[$key[$i]];
			$test_broker_id = $current_broker_id;
			
			// // Test
			if(is_numeric($test_broker_id))
			{
				// Insert MYSQL
				mysql_query("INSERT INTO house_raw_danbolig (id, house_id, broker_id, created_at) VALUES (NULL, '$current_house_id', '$current_broker_id', NOW())") or mysql_error();	
				$processed++;
			}
					
			// Next in array
			$current_house_id = next($this->house_id);
			
			// Count
			$found++;
			$this->count++;
			$this->remaining--;
		}
		
		// Add to console
		$this->time->stop();
		$this->time1->stop();
		
		$this->console_add("Page ".$this->page." - ".$found." Found / ".$processed." Processed - ".($this->remaining+1)." Remaining - ".$this->time->view(2)." / ".$this->time1->view(2)." sec");
	}		
	
	function console_print () {
		echo "\n";
		echo "============================================================================\n";
		echo "| Fetch Program Beta for Danbolig - By Xiplias     \n";
		echo "============================================================================\n";	
		echo "|  Total Houses to process: ".$this->remaining."  \n|\n";
		echo "|  ID Fetching:\n";
		echo "----------------------------------------------------------------------------\n";
	}
	
	function console_add ($line) {
		echo "|   ".$line."\n";
	}
}

?>
